package com.sh.data.engine.web.openapi.controller.isfd;

import com.sh.data.engine.application.model.dto.Isfd.IsfdModelDTO;
import com.sh.data.engine.application.model.dto.Isfd.IsfdModelUploadDTO;
import com.sh.data.engine.application.model.dto.Isfd.IsfdStaticStrengthAnalysisMatDTO;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.request.Isfd.IsfdModelRequest;
import com.sh.data.engine.application.model.request.Isfd.IsfdstaticStrengthAnalysisRequest;
import com.sh.data.engine.domain.feign.Service.IsfdModelFileUploadService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sh.data.engine.application.service.Isfd.IsfdApplicationService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/openapi/isfd/model")
@Tag(name = "要素库模型", description = "要素库模型相关接口")
public class IsfdModelController {
    @Autowired
    private IsfdApplicationService isfdApplicationService;

//    @Autowired
//    private IsfdTokenService isfdTokenService;

//    @PostMapping("/getIsfdToken")
//    @Operation(summary = "获取第三方服务用户Token", description = "该api用于获取第三方服务用户Token")
//    public RestResponseEntity<String> getToken() {
//        return RestResponseEntity.success(isfdTokenService.getToken());
//    }

    @GetMapping("/listModels")
    @Operation(summary = "获取要素库模型列表", description = "该api用于获取要素库模型列表")
    public RestResponseEntity<PagerResponse<IsfdModelDTO>> listModels(IsfdModelRequest request) {
        PagerResponse<IsfdModelDTO> elmModelList = isfdApplicationService.getElmModelList(request);
        return RestResponseEntity.success(elmModelList);
    }

    @PostMapping("/uploadModelFile")
    public RestResponseEntity<IsfdModelUploadDTO> uploadModel(
            @RequestParam("file") MultipartFile file,
            @RequestParam("modelName") String modelName,
            @RequestParam("modelType") String modelType,
            @RequestParam("applicableModels") String applicableModels,
            @RequestParam("modelSeries") String modelSeries,
            @RequestParam("loadConditions") String loadConditions,
            @RequestParam("modelScope") String modelScope,
            @RequestParam("businessScenarios") String businessScenarios,
            @RequestParam("modelDescription") String modelDescription,
            @RequestParam("sourceMethods") String sourceMethods) throws IOException {

        // 将MultipartFile转换为临时文件
        Path tempFile = Files.createTempFile("upload-", file.getOriginalFilename());
        file.transferTo(tempFile);

        try {
            // 准备表单参数
            Map<String, String> formParams = new HashMap<>();
            formParams.put("modelName", modelName);
            formParams.put("modelType", modelType);
            formParams.put("applicableModels", applicableModels);
            formParams.put("modelSeries", modelSeries);
            formParams.put("loadConditions", loadConditions);
            formParams.put("modelScope", modelScope);
            formParams.put("businessScenarios", businessScenarios);
            formParams.put("modelDescription", modelDescription);
            formParams.put("sourceMethods", sourceMethods);

            // 调用上传服务
            IsfdModelUploadDTO isfdModelUploadDTO = isfdApplicationService.uploadModelFile(tempFile.toFile(), formParams);
            return RestResponseEntity.success(isfdModelUploadDTO);
        } finally {
            // 删除临时文件
            Files.deleteIfExists(tempFile);
        }
    }
}
